07. Exercise: Sorting Collections
Exercise: Sorting Collections
Create a Driver program
Task Description:
Follow these steps to create a Driver program to see how sorting can be done with Collections.
Task Feedback:
Nice work!
Solution
Nd079 C1 L5 A03b Sorting Exercise
Here is the solution for the exercise. Notice how easy it is to sort elements with the Collections utility class!
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class SortingExample {
public static void main(String[] args) {
List<String> names = new LinkedList<String>();
names.add("Mike");
names.add("Bob");
names.add("Alice");
//Before Sorting
for (String name : names) {
System.out.println(name);
}
//sorting
Collections.sort(names);
//after sorting
for (String name : names) {
System.out.println(name);
}
}
}